package com.zzxx.controller;

import com.zzxx.entity.Coupon;
import com.zzxx.entity.User;
import com.zzxx.entity.UserCouponInfo;
import com.zzxx.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("coupon")
public class CouponController {
    @Autowired
    private CouponService CouponService;

    @Autowired(required = false)
    private HttpServletRequest request;

    @Autowired
    private CouponService couponService;
    @RequestMapping("findCoupon")
    public Double findCoupon(String id){
     Coupon c= couponService.find(id);
        Double number = c.getNumber();
        return number;
    }

    @RequestMapping(value = "findList/{uid}", method = RequestMethod.GET)
    public UserCouponInfo findList(@PathVariable("uid") String userId){
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        if (loginUser==null||!userId.equals(loginUser.getId())) return null;
        UserCouponInfo info = new UserCouponInfo();
        info.setUser(loginUser);
        CouponService.findUserCouponInfo(info);
        return info;
    }
    @RequestMapping("findAll")
    public List<Coupon> findAll(String uid){
      List<Coupon> coupons= couponService.findAll(uid);
      return coupons;
    }
    @RequestMapping("addCoupon")
    public String addCoupon(String id){
        couponService.addCoupon(id);
        return "ok";
    }
}
